//
//  ElevenViewController.swift
//  swiftTS
//
//  Created by 航电 on 2020/4/14.
//  Copyright © 2020 航电. All rights reserved.
//

import UIKit

fileprivate let cellID = "Eleven_CellID";

class ElevenViewController: UIViewController {

    fileprivate lazy var mutDataArr:NSMutableArray = {
        var mutArr = NSMutableArray();
        mutArr.add("flowLayout");
        return mutArr;
    }();
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "UICollection使用";
        self.view.backgroundColor = .white;
        // Do any additional setup after loading the view.
        self.onCreateUIForNavigation();
        self.onCreateUIForView();
    }
    

    //MARK:UI
    fileprivate func onCreateUIForNavigation() {
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", style: .done, target: self, action: #selector(onBack));
    }
    
    fileprivate func onCreateUIForView() {
        let tableView = UITableView(frame: CGRect(x: 0, y: 0, width: screenMainSize.width, height: screenMainSize.height), style: .plain);
        tableView.backgroundColor = .clear;
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.showsHorizontalScrollIndicator = false;
        tableView.showsVerticalScrollIndicator = false;
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: cellID);
        self.view.addSubview(tableView);
    }
    
    //MARK:Actions
    @objc fileprivate func onBack() {
        self.navigationController?.dismiss(animated: true, completion: nil);
    }

}

extension ElevenViewController:UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.mutDataArr.count;
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: cellID);
        
        if self.mutDataArr.count > indexPath.row {
            cell?.textLabel?.text = "\(mutDataArr[indexPath.row])";
        }
        return cell!;
    }
}

extension ElevenViewController:UITableViewDelegate {
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if mutDataArr.count > indexPath.row {
            let vc = ElevenFlowLayoutViewController();
            self.navigationController?.pushViewController(vc, animated: true);
        }
    }
}
